import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";
import { login } from "../../utils/asyncWx.js";

Page({
  data: {
    openId: 12333,
  },
  // 获取用户信息
  async handleGetUserInfo() {
    let { openId } = this.data;
    try {
      // 1 获取用户信息
      // const { encryptedData, rawData, iv, signature } = e.detail;
      // 2 获取小程序登录成功后的code
      const { code } = await login();
      console.log("code", code);
      // wx.setStorageSync("")
      // const loginParams={ encryptedData, rawData, iv, signature ,code};
      //  3 发送请求 获取用户的token
      // const res=await request({url:"/login",openId:code,method:"get"});
      // let openId = code;
      console.log("openID", openId);
      const res = await request({
        url: "/v1/login",
        data: { openId },
      });
      console.log("data", res);
      // 4 把token存入缓存中 同时跳转回上一个页面
      wx.setStorageSync("token", res);
      wx.navigateBack({
        delta: 1,
      });
    } catch (error) {
      console.log(error);
    }
  },
});
